package com.yunxi.service.localService.impl.sto;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.github.pagehelper.PageInfo;
import com.yunxi.config.ReadDataSource;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.sto.DailyReportMapper;
import com.yunxi.model.sto.DailyReport;
import com.yunxi.service.localService.sto.DailyReportService;
import com.yunxi.service.localService.sto.ReportPartnerDayService;
import com.yunxi.service.localService.sto.ReportPartnerMonthService;
import com.yunxi.service.localService.sto.ReportPartnerYearService;
import com.yunxi.sto.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.sql.CallableStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 基础报告日报表 服务实现类
 * </p>
 *
 * @author caoYong
 * @since 2019-09-20
 */
@Service
public class DailyReportServiceImpl extends ServiceImplPlus<DailyReportMapper, DailyReport> implements DailyReportService {

    @Autowired
    @Qualifier(value = "dailyReportServiceImpl")
    DailyReportService dailyReportService;

    @Autowired
    @Qualifier(value = "reportPartnerYearServiceImpl")
    ReportPartnerYearService reportPartnerYearService;

    @Autowired
    @Qualifier(value = "reportPartnerMonthServiceImpl")
    ReportPartnerMonthService reportPartnerMonthService;

    @Autowired
    @Qualifier(value = "reportPartnerDayServiceImpl")
    ReportPartnerDayService reportPartnerDayService;

    @Autowired
    ReadDataSource readDataSource;

    @Override
    public PageInfo<MonthlyReportVO> monthPage3(MonthlyReportVO monthlyReportVO, Integer pageNum, Integer pageSize) {

        if(monthlyReportVO.getIsReset()==null) monthlyReportVO.setIsReset(0);

        //Long has = reportPartnerMonthService.getByReportDate(monthlyReportVO.getCurYearMonth());
        if(monthlyReportVO.getIsReset()==1){
            baseMapper.monthCalcMain(monthlyReportVO);
        }

        ReportPartnerMonthVO reportPartnerMonth = new ReportPartnerMonthVO();
        reportPartnerMonth.setReportDate(monthlyReportVO.getCurYearMonth());
        reportPartnerMonth.setBuyType(monthlyReportVO.getBuyType());
        return reportPartnerMonthService.monthList3(reportPartnerMonth,pageNum,pageSize);
    }

    @Override
    public PageInfo<DailyReportVO> dayPage(DailyReportVO dailyReportVO, Integer pageNum, Integer pageSize) {
        return reportPartnerDayService.dayPage(dailyReportVO,pageNum,pageSize);
    }

    @Override
    public void dayCalcMain(DailyReportVO dailyReportVO) throws SQLException {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(readDataSource.getUrl());
        dsc.setDriverName(readDataSource.getDriverClassName());
        dsc.setUsername(readDataSource.getUsername());
        dsc.setPassword(readDataSource.getPassword());
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            public DbColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
                return (DbColumnType) super.processTypeConvert(gc,fieldType);
            }
        });

        java.sql.Connection conn = dsc.getConn();
        String sql = "{call calc_day_main(?)}";
        CallableStatement cs = conn.prepareCall(sql);  //调用格式 {call 存储过程名(参数)}
        cs.setString(1, dailyReportVO.getQueryDate());
        cs.executeQuery();            //执行
        cs.close();
        conn.close();
    }

    @Override
    public Long monthTargetAmount(String queryDate) {
        return baseMapper.monthTargetAmount(queryDate);
    }

    @Override
    public Long monthTargetOrderAmount(String queryDate) {
        return baseMapper.monthTargetOrderAmount(queryDate);
    }

    @Override
    public PageInfo<YearReportVO> yearList3(YearReportVO yearReportVO, Integer pageNum, Integer pageSize) {

        if(yearReportVO.getIsReset()==null) yearReportVO.setIsReset(0);

        //Long has = reportPartnerYearService.getByReportDate(yearReportVO.getCurYear());
        if(yearReportVO.getIsReset()==1){
            baseMapper.yearCalcMain(yearReportVO);
        }

        ReportPartnerYearVO reportPartnerYear = new ReportPartnerYearVO();
        reportPartnerYear.setReportDate(yearReportVO.getCurYear());
        reportPartnerYear.setBuyType(yearReportVO.getBuyType());
        reportPartnerYear.setPartnerCode(yearReportVO.getPartnerCode());
        reportPartnerYear.setReportDate(yearReportVO.getCurYear());
        return reportPartnerYearService.yearList3(reportPartnerYear,pageNum,pageSize);
    }

    @Override
    public void monthCalcMain(MonthlyReportVO curYear) throws SQLException {

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(readDataSource.getUrl());
        // dsc.setSchemaName("public");
        dsc.setDriverName(readDataSource.getDriverClassName());
        dsc.setUsername(readDataSource.getUsername());
        dsc.setPassword(readDataSource.getPassword());
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            public DbColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
                return (DbColumnType) super.processTypeConvert(gc,fieldType);
            }
        });

        java.sql.Connection conn = dsc.getConn();
        String sql = "{call calc_month_main(?,?,?,?)}";
        CallableStatement cs = conn.prepareCall(sql);  //调用格式 {call 存储过程名(参数)}
        cs.setString(1, curYear.getCurYearMonth());
        cs.setString(2, curYear.getQueryDate());
        cs.setLong(3, curYear.getTargetAmount());
        cs.setLong(4, curYear.getTargetOrderAmount());
        cs.executeQuery();            //执行
        cs.close();
        conn.close();
    }

    @Override
    public void yearCalcMain(YearReportVO curYear) throws SQLException {

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(readDataSource.getUrl());
        // dsc.setSchemaName("public");
        dsc.setDriverName(readDataSource.getDriverClassName());
        dsc.setUsername(readDataSource.getUsername());
        dsc.setPassword(readDataSource.getPassword());
        dsc.setDbType(DbType.MYSQL);
        dsc.setTypeConvert(new MySqlTypeConvert(){
            // 自定义数据库表字段类型转换【可选】
            public DbColumnType processTypeConvert(GlobalConfig gc, String fieldType) {
                System.out.println("转换类型：" + fieldType);
                // 注意！！processTypeConvert 存在默认类型转换，如果不是你要的效果请自定义返回、非如下直接返回。
                return (DbColumnType) super.processTypeConvert(gc,fieldType);
            }
        });

        java.sql.Connection conn = dsc.getConn();
        String sql = "{call calc_year_main(?)}";
        CallableStatement cs = conn.prepareCall(sql);  //调用格式 {call 存储过程名(参数)}
        cs.setString(1, curYear.getCurYear());
        cs.executeQuery();            //执行
        cs.close();
        conn.close();
        //baseMapper.yearCalcMain(curYear);
    }
}
